#!/usr/bin/env python3  /*设定脚本文件直接能执行*/
# -*- coding:utf-8 -*-   /*设置编码*/
"""
@Author: WANDERSON M.PIMENTA
@Software: PyCharm 、Qt Designer and PySide6
@File: tQLeftMenuButton.py
@Time: 2024/05/11 16:35
@Function：ctQLeftMenuButton：左侧菜单按键（隐藏按键）
"""
import os
from tQQtCore import *
from UI.cores.tFunctions import *


# 左侧菜单按键（隐藏按键）
class ctQLeftMenuButton(QPushButton):
    # 初始化
    def __init__(self,
                 app_parent,
                 text,
                 btn_id=None,
                 tooltip_text="",
                 margin=4,
                 dark_one="#1b1e23",
                 dark_three="#21252d",
                 dark_four="#272c36",
                 bg_one="#2c313c",
                 icon_color="#c3ccdf",
                 icon_color_hover="#dce1ec",
                 icon_color_pressed="#edf0f5",
                 icon_color_active="#f5f6f9",
                 context_color="#568af2",
                 text_foreground="#8a95aa",
                 text_active="#dce1ec",
                 icon_path="icon_add_user.svg",
                 icon_active_menu="active_menu.svg",
                 is_active=False,
                 is_active_tab=False,
                 is_toggle_active=False
                 ):
        super().__init__()
        # 设置文本
        self.setText(text)
        self.setCursor(Qt.PointingHandCursor)
        # 设置大小
        self.setMaximumHeight(50)
        self.setMinimumHeight(50)
        self.setObjectName(btn_id)

        # Icon路径
        self._icon_path = Functions.f_getSvgIconPath(icon_path)
        self._icon_active_menu = Functions.f_getSvgIconPath(icon_active_menu)

        # <editor-fold desc="设置属性">
        self._margin = margin
        self._dark_one = dark_one
        self._dark_three = dark_three
        self._dark_four = dark_four
        self._bg_one = bg_one
        self._context_color = context_color
        self._icon_color = icon_color
        self._icon_color_hover = icon_color_hover
        self._icon_color_pressed = icon_color_pressed
        self._icon_color_active = icon_color_active
        self._set_icon_color = self._icon_color  # Set icon color
        self._set_bg_color = self._dark_one  # Set BG color
        self._set_text_foreground = text_foreground
        self._set_text_active = text_active
        self._parent = app_parent
        self._is_active = is_active
        self._is_active_tab = is_active_tab
        self._is_toggle_active = is_toggle_active
        # </editor-fold>

        # <editor-fold desc="设置提示框">
        self._tooltip_text = tooltip_text
        self.tooltip = _ToolTip(app_parent,
                                tooltip_text,
                                dark_one,
                                context_color,
                                text_foreground
                                )
        self.tooltip.hide()
        # </editor-fold>

    # 设置菜单激活状态
    def f_setMenuActive(self, is_active):
        """
        function:  设置菜单激活状态
              in:  is_active:True or False
             out:  None
          return:  None
          others:  Set Menu Activation State
        """
        self._is_active = is_active
        if not is_active:
            self._set_icon_color = self._icon_color
            self._set_bg_color = self._dark_one
        self.repaint()

    # 设置切换(隐藏)开关激活状态
    def f_setToggleActive(self, is_active):
        """
        function:  设置切换(隐藏)开关激活状态
              in:  is_active:True or False
             out:  None
          return:  None
          others:  Set The Toggle (Hide) Switch Activation State
        """
        self._is_toggle_active = is_active

    # 设置Tab页激活状态
    def f_setTabActive(self, is_active):
        """
        function:  设置Tab页激活状态
              in:  is_active:True or False
             out:  None
          return:  None
          others:  Set Tab Page Activation State
        """
        self._is_active_tab = is_active
        if not is_active:
            self._set_icon_color = self._icon_color
            self._set_bg_color = self._dark_one
        # 重新绘制
        self.repaint()

    # 获取菜单激活状态
    def f_getMenuActive(self):
        """
        function:  获取菜单激活状态
              in:  None
             out:  None
          return:  bool
          others:  Get Menu Activation State
        """
        return self._is_active

    # 获取Tab页激活状态
    def f_getTabActive(self):
        """
        function:  获取Tab页激活状态
              in:  None
             out:  None
          return:  bool
          others:  Get Tab Page Activation State
        """
        return self._is_active_tab

    # 设置按键的图标
    def f_setIcon(self, icon_path):
        """
        function:  设置切换(隐藏)开关的图标
              in:  icon_path
             out:  None
          return:  bool
          others:  Set The Toggle (Hide) Switch Icon
        """
        self._icon_path = icon_path
        self.repaint()

    # <editor-fold desc="重写事件">
    # 重写绘图事件
    def paintEvent(self, event):
        # PAINTER
        p = QPainter()
        p.begin(self)
        p.setRenderHint(QPainter.Antialiasing)
        p.setPen(Qt.NoPen)
        p.setFont(self.font())

        # RECTANGLES
        rect = QRect(4, 5, self.width(), self.height() - 10)
        rect_inside = QRect(4, 5, self.width() - 8, self.height() - 10)
        rect_icon = QRect(0, 0, 50, self.height())
        rect_blue = QRect(4, 5, 20, self.height() - 10)
        rect_inside_active = QRect(7, 5, self.width(), self.height() - 10)
        rect_text = QRect(45, 0, self.width() - 50, self.height())

        if self._is_active:
            # DRAW BG BLUE
            p.setBrush(QColor(self._context_color))
            p.drawRoundedRect(rect_blue, 8, 8)

            # BG INSIDE
            p.setBrush(QColor(self._bg_one))
            p.drawRoundedRect(rect_inside_active, 8, 8)

            # DRAW ACTIVE
            icon_path = self._icon_active_menu
            app_path = os.path.abspath(os.getcwd())
            icon_path = os.path.normpath(os.path.join(app_path, icon_path))
            self._set_icon_color = self._icon_color_active
            self.__f_iconActive(p, icon_path, self.width())

            # DRAW TEXT
            p.setPen(QColor(self._set_text_active))
            p.drawText(rect_text, Qt.AlignVCenter, self.text())

            # DRAW ICONS
            self.__f_iconPaint(p, self._icon_path, rect_icon, self._set_icon_color)

        elif self._is_active_tab:
            # DRAW BG BLUE
            p.setBrush(QColor(self._dark_four))
            p.drawRoundedRect(rect_blue, 8, 8)

            # BG INSIDE
            p.setBrush(QColor(self._bg_one))
            p.drawRoundedRect(rect_inside_active, 8, 8)

            # DRAW ACTIVE
            icon_path = self._icon_active_menu
            app_path = os.path.abspath(os.getcwd())
            icon_path = os.path.normpath(os.path.join(app_path, icon_path))
            self._set_icon_color = self._icon_color_active
            self.__f_iconActive(p, icon_path, self.width())

            # DRAW TEXT
            p.setPen(QColor(self._set_text_active))
            p.drawText(rect_text, Qt.AlignVCenter, self.text())

            # DRAW ICONS
            self.__f_iconPaint(p, self._icon_path, rect_icon, self._set_icon_color)

        # NORMAL BG
        else:
            if self._is_toggle_active:
                # BG INSIDE
                p.setBrush(QColor(self._dark_three))
                p.drawRoundedRect(rect_inside, 8, 8)

                # DRAW TEXT
                p.setPen(QColor(self._set_text_foreground))
                p.drawText(rect_text, Qt.AlignVCenter, self.text())

                # DRAW ICONS
                if self._is_toggle_active:
                    self.__f_iconPaint(p, self._icon_path, rect_icon, self._context_color)
                else:
                    self.__f_iconPaint(p, self._icon_path, rect_icon, self._set_icon_color)
            else:
                # BG INSIDE
                p.setBrush(QColor(self._set_bg_color))
                p.drawRoundedRect(rect_inside, 8, 8)

                # DRAW TEXT
                p.setPen(QColor(self._set_text_foreground))
                p.drawText(rect_text, Qt.AlignVCenter, self.text())

                # DRAW ICONS
                self.__f_iconPaint(p, self._icon_path, rect_icon, self._set_icon_color)

        p.end()

    # 重写鼠标进入按键事件
    def enterEvent(self, event):
        self.__f_changeStyle(QEvent.Enter)
        if self.width() == 50 and self._tooltip_text:
            self.__f_moveTooltip()
            self.tooltip.show()

    # 重写鼠标离开按键事件
    def leaveEvent(self, event):
        self.__f_changeStyle(QEvent.Leave)
        self.tooltip.hide()

    # 重写鼠标离按下按键事件
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.__f_changeStyle(QEvent.MouseButtonPress)
            self.tooltip.hide()
            return self.clicked.emit()

    # 重写鼠标离绘制按键事件
    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.__f_changeStyle(QEvent.MouseButtonRelease)
            return self.released.emit()

    # </editor-fold>

    # <editor-fold desc="该类用到的私有函数">
    # 绘制Icon
    @staticmethod
    def __f_iconPaint(qp, image, rect, color):
        icon = QPixmap(image)
        painter = QPainter(icon)
        painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
        painter.fillRect(icon.rect(), color)
        qp.drawPixmap(
            (rect.width() - icon.width()) / 2,
            (rect.height() - icon.height()) / 2,
            icon
        )
        painter.end()

    # 绘制激活Icon
    def __f_iconActive(self, qp, image, width):
        icon = QPixmap(image)
        painter = QPainter(icon)
        painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
        painter.fillRect(icon.rect(), self._bg_one)
        qp.drawPixmap(width - 5, 0, icon)
        painter.end()

    # 移除提示框
    def __f_moveTooltip(self):
        # 获取主窗口父窗口
        gp = self.mapToGlobal(QPoint(0, 0))

        # 返回部件在应用程序中的绝对位置
        pos = self._parent.mapFromGlobal(gp)

        # 用偏移量调整工具提示位置
        pos_x = pos.x() + self.width() + 5
        pos_y = pos.y() + (self.width() - self.tooltip.height()) // 2

        # 移动提示框位置
        self.tooltip.move(pos_x, pos_y)

    # 更改样式表
    def __f_changeStyle(self, event):
        if event == QEvent.Enter:
            if not self._is_active:
                self._set_icon_color = self._icon_color_hover
                self._set_bg_color = self._dark_three
            self.repaint()
        elif event == QEvent.Leave:
            if not self._is_active:
                self._set_icon_color = self._icon_color
                self._set_bg_color = self._dark_one
            self.repaint()
        elif event == QEvent.MouseButtonPress:
            if not self._is_active:
                self._set_icon_color = self._context_color
                self._set_bg_color = self._dark_four
            self.repaint()
        elif event == QEvent.MouseButtonRelease:
            if not self._is_active:
                self._set_icon_color = self._icon_color_hover
                self._set_bg_color = self._dark_three
            self.repaint()

    # </editor-fold>


# 提示框
class _ToolTip(QLabel):
    # 标签样式表
    style_tooltip = """ 
    QLabel {{		
        background-color: {_dark_one};	
        color: {_text_foreground};
        padding-left: 10px;
        padding-right: 10px;
        border-radius: 17px;
        border: 0px solid transparent;
        border-left: 3px solid {_context_color};
        font: 800 9pt "Segoe UI";
    }}
    """

    # 初始化
    def __init__(self,
                 parent,
                 tooltip,
                 dark_one,
                 context_color,
                 text_foreground
                 ):
        QLabel.__init__(self)

        # 设置标签样式
        style = self.style_tooltip.format(_dark_one=dark_one,
                                          _context_color=context_color,
                                          _text_foreground=text_foreground
                                          )
        self.setObjectName(u"label_tooltip")
        self.setStyleSheet(style)
        self.setMinimumHeight(34)
        self.setParent(parent)
        self.setText(tooltip)
        self.adjustSize()

        # 设置阴影
        self.shadow = QGraphicsDropShadowEffect(self)
        self.shadow.setBlurRadius(30)
        self.shadow.setXOffset(0)
        self.shadow.setYOffset(0)
        self.shadow.setColor(QColor(0, 0, 0, 80))
        self.setGraphicsEffect(self.shadow)
